package com.gdkf.dormitory.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.gdkf.dormitory.entity.Excel;
import com.gdkf.dormitory.entity.MyPage;
import com.gdkf.dormitory.entity.Student;
import com.gdkf.dormitory.service.StudentService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.tools.ObjectQuery;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/student/getAllStudent")
    public Msg getAllStudent(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        Page<Student> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        studentService.page(page, null);
        List<Student> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("studentList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/student/getOneStudent")
    public Msg getOneStudent(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToStudent(myPage);
            Page<Student> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            wrapper.eq("stu_id",((Student)myPage2.getObject()).getStuId());
            Page<Student> page1 = studentService.page(page, wrapper);
            List<Student> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("studentList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();


    }

    @PostMapping("/student/getSomeStudentByName")
    public Msg getSomeStudentByName(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToStudent(myPage);
            System.out.println(myPage2);
            Page<Student> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            String stuName = ((Student) myPage2.getObject()).getStuName();
            wrapper.like(!("".equals(stuName)),"stu_name",stuName);
            Page<Student> page1 = studentService.page(page, wrapper);
            List<Student> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("studentList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();

    }

    @PostMapping("/student/addStudent")
    public Msg addStudent(@RequestBody Student student){
        System.out.println(student);
        boolean save = studentService.save(student);
        return save?Msg.success():Msg.fail();
    }

    @PostMapping("/student/updateStudent")
    public Msg updateStudent(@RequestBody Student student){
        System.out.println("=====updateStudent=====>"+student);
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id",student.getStuId());
        return studentService.update(student,wrapper)?Msg.success():Msg.fail();
    }

    @PostMapping("/student/deleteStudent")
    public Msg deleteStudent(@RequestBody Student student){
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id",student.getStuId());
        return studentService.remove(wrapper)?Msg.success():Msg.fail().add("Possible Errors","处理失败，请检查是否有关联数据未删除");
    }

    @PostMapping ("/student/exportExcel")
    public Msg exportExcel(@RequestBody Excel excel){
        System.out.println(excel);
        List<Student> list = studentService.list();
        if(!("".equals(excel.getExcelFileName())) && !("".equals(excel.getExcelFilePath()))){
            String fileName = excel.getExcelFilePath() + excel.getExcelFileName()+ ".xlsx";
            EasyExcel.write(fileName, Student.class).sheet("学生表").doWrite(list);
            return Msg.success()
                    .add("fileSave",excel.getExcelFilePath() + excel.getExcelFileName()+ ".xlsx")
                    .add("fileSaveTips","小提示：若传入的是相对路径，请以本系统为根路径寻找");
        }
        return Msg.fail();
    }
    @PostMapping("/student/chartDepData")
    public Msg chartDepData(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("tableName","student");
        map.put("tableColunm","stu_dep");
        List<ObjectQuery> objectQueries = studentService.selectByGroup(map);
        return Msg.success()
                .add("chartDataList",objectQueries);

    }
    @PostMapping("/student/chartClassData")
    public Msg chartClassData(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("tableName","student");
        map.put("tableColunm","stu_class");
        List<ObjectQuery> objectQueries = studentService.selectByGroup(map);
        return Msg.success()
                .add("chartDataList",objectQueries);

    }
    @PostMapping("/student/chartSexData")
    public Msg chartSexData(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("tableName","student");
        map.put("tableColunm","stu_sex");
        List<ObjectQuery> objectQueries = studentService.selectByGroup(map);
        return Msg.success()
                .add("chartDataList",objectQueries);

    }
    @PostMapping("/student/getAllStudentId")
    public Msg getAllStudentId(){
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.select("stu_id");
        List<Student> stuIdList = studentService.list(wrapper);
        return stuIdList.isEmpty()?Msg.fail():Msg.success().add("stuIdList",stuIdList);
    }

}

